<?php
namespace app\api\validate;

use think\Validate;

class Base extends Validate
{
    // 根据id判断是否存在，存在将实例加入request
    protected function isExist($value, $rule, $data='', $field='',$title = '记录'){
        $arr = explode(',',$rule);
        if (!$value) return true;
        $Model = count($arr) > 1 ? '\\app\\model\\'.$arr[1] :'\\app\\model\\'.str_replace('.','\\',request()->controller());
        $M = $Model::find($value);
        if (!$M) {
            return '该'.$title.'不存在';
        }
        // 将当前实例挂在到Request上
        if ($arr[0] !== 'false') request()->Model = $M;

        return true;
    }
}